<?php
class ProjectViewsController extends AppController
{
    public $name = 'ProjectViews';
    public function admin_index()
    {
        $this->_redirectGET2Named(array(
            'project_id',
            'q'
        ));
        $this->pageTitle = __l('Project Views');
        $conditions = array();
        // check the filer passed through named parameter
        if (isset($this->request->params['named']['stat']) && $this->request->params['named']['stat'] == 'day') {
            $conditions['TO_DAYS(NOW()) - TO_DAYS(ProjectView.created) <= '] = 0;
            $this->pageTitle.= __l(' - today');
        }
        if (isset($this->request->params['named']['stat']) && $this->request->params['named']['stat'] == 'week') {
            $conditions['TO_DAYS(NOW()) - TO_DAYS(ProjectView.created) <= '] = 7;
            $this->pageTitle.= __l(' - in this week');
        }
        if (isset($this->request->params['named']['stat']) && $this->request->params['named']['stat'] == 'month') {
            $conditions['TO_DAYS(NOW()) - TO_DAYS(ProjectView.created) <= '] = 30;
            $this->pageTitle.= __l(' - in this month');
        }
        if (!empty($this->request->params['named']['project'])) {
            $project = $this->ProjectView->Project->find('first', array(
                'conditions' => array(
                    'Project.slug' => $this->request->params['named']['project']
                ) ,
                'fields' => array(
                    'Project.id',
                    'Project.name'
                ) ,
                'recursive' => - 1
            ));
            $conditions['ProjectView.project_id'] = $project['Project']['id'];
            $this->pageTitle.= ' - ' . $project['Project']['name'];
        }
        if (isset($this->request->params['named']['q'])) {
            $conditions['AND']['OR'][]['Project.name LIKE'] = '%' . $this->request->params['named']['q'] . '%';
            $conditions['AND']['OR'][]['User.username LIKE'] = '%' . $this->request->params['named']['q'] . '%';
            $this->pageTitle.= sprintf(__l(' - Search - %s') , $this->request->params['named']['q']);
        }
        if (!empty($this->request->params['named']['project_id'])) {
            $conditions['ProjectView.project_id'] = $this->request->params['named']['project_id'];
            $project_name = $this->ProjectView->Project->find('first', array(
                'conditions' => array(
                    'Project.id' => $this->request->params['named']['project_id'],
                ) ,
                'fields' => array(
                    'Project.name',
                ) ,
                'recursive' => - 1,
            ));
            $this->pageTitle.= sprintf(__l(' - %s') , $project_name['Project']['name']);
        }
		if(!empty($this->request->params['named']['type'])){
			if($this->request->params['named']['type'] == 'embed'){
			  $conditions['ProjectView.project_view_type_id'] = ConstViewType::EmbedView;
			}else{
				$conditions['ProjectView.project_view_type_id'] = ConstViewType::NormalView;
			}
		}
        $this->ProjectView->recursive = 0;
        $this->paginate = array(
            'conditions' => $conditions,
            'contain' => array(
                'User' => array(
                    'fields' => array(
                        'User.username'
                    )
                ) ,
                'Project' => array(
                    'ProjectStatus',
                ),
						'Ip' => array(
                    'City' => array(
                        'fields' => array(
                            'City.name',
                        )
                    ) ,
                    'State' => array(
                        'fields' => array(
                            'State.name',
                        )
                    ) ,
                    'Country' => array(
                        'fields' => array(
                            'Country.name',
                            'Country.iso2',
                        )
                    ) ,
                    'fields' => array(
                        'Ip.ip',
                        'Ip.latitude',
                        'Ip.longitude',
						'Ip.host'
                    )
                ) , 
            ) ,
            'order' => array(
                'ProjectView.id' => 'desc'
            ) ,
        );
        $this->set('projectViews', $this->paginate());
        $moreActions = $this->ProjectView->moreActions;
        $this->set(compact('moreActions'));
    }
    public function admin_delete($id = null)
    {
        if (is_null($id)) {
            throw new NotFoundException(__l('Invalid request'));
        }
        if ($this->ProjectView->delete($id)) {
            $this->Session->setFlash(__l('Project View deleted') , 'default', null, 'success');
            $this->redirect(array(
                'action' => 'index'
            ));
        } else {
            throw new NotFoundException(__l('Invalid request'));
        }
    }
    public function admin_update()
    {
        if (!empty($this->request->data[$this->modelClass])) {
            $r = $this->request->data[$this->modelClass]['r'];
            $actionid = $this->request->data[$this->modelClass]['more_action_id'];
            unset($this->request->data[$this->modelClass]['r']);
            unset($this->request->data[$this->modelClass]['more_action_id']);
            $selectedIds = array();
            foreach($this->request->data[$this->modelClass] as $primary_key_id => $is_checked) {
                if ($is_checked['id']) {
                    $selectedIds[] = $primary_key_id;
                }
            }
            if ($actionid && !empty($selectedIds)) {
                if ($actionid == ConstMoreAction::Delete) {
                    $this->{$this->modelClass}->deleteAll(array(
                        $this->modelClass . '.id' => $selectedIds
                    ));
                    $this->Session->setFlash(__l('Checked project views has been deleted') , 'default', null, 'success');
                }
            }
        }
        $this->redirect(Router::url('/', true) . $r);
    }
}
?>